<template>
  <div id="manageQuestionView">
    <h2>管理题目</h2>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: queryParams.pageSize,
        current: queryParams.current,
        total,
      }"
      @page-change="onPageChange"
    >
      <template #optional="{ record }">
        <a-space>
          <a-button type="primary" @click="doUpdate(record)">修改</a-button>
          <a-button status="danger" @click="doDelete(record)">删除</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<style scoped>
#manageQuestionView {
  padding: 16px;
}
</style>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import { QuestionControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";

const dataList = ref([]);
const total = ref(0);
const queryParams = ref({
  pageSize: 5,
  current: 1,
});
const loadData = async () => {
  const res = await QuestionControllerService.listQuestionByPageUsingPost(
    queryParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("获取失败" + res.message);
  }
};

onMounted(() => {
  loadData();
});

const onPageChange = (current: number) => {
  queryParams.value = {
    ...queryParams.value,
    current: current,
  };
};

//监听queryParams变量改变
watchEffect(() => {
  loadData();
});

const doDelete = async (question: any) => {
  const res = await QuestionControllerService.deleteQuestionUsingPost({
    id: question.id,
  });
  if (res.code === 0) {
    message.success("删除题目成功");
    loadData();
  } else {
    message.error("删除题目失败" + res.message);
  }
};

const router = useRouter();
const doUpdate = (question: any) => {
  console.log(question);
  router.push({
    path: "/update/question",
    query: {
      id: question.id,
    },
  });
};
//
const columns = [
  {
    title: "id",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "内容",
    dataIndex: "content",
  },
  {
    title: "标签",
    dataIndex: "tags",
  },
  {
    title: "答案",
    dataIndex: "answer",
  },
  {
    title: "用户ID",
    dataIndex: "userId",
  },
  {
    title: "提交数",
    dataIndex: "submitNum",
  },
  {
    title: "接受数",
    dataIndex: "acceptedNum",
  },
  {
    title: "收藏数",
    dataIndex: "favourNum",
  },
  {
    title: "点赞数",
    dataIndex: "thumbNum",
  },
  {
    title: "判题用例",
    dataIndex: "judgeCase",
  },
  {
    title: "判题配置",
    dataIndex: "judgeConfig",
  },
  {
    title: "创建时间",
    dataIndex: "createTime",
  },
  {
    title: "操作",
    slotName: "optional",
  },
];
</script>
